import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// 按需引入 Element-Plus
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// 引入 vite-plugin-pages 自动生成路由
import Pages from  'vite-plugin-pages'
// 引入 vite-plugin-vue-layouts 布局系统
import Layouts from 'vite-plugin-vue-layouts';

export default defineConfig({
  plugins: [
    vue(),
    Pages({
      dirs:[
        // 默认文件夹为pages，我的项目是views，需要配置
        { dir: "src/views", baseRoute: "" }
      ],
      importMode: "async"
    }),
    Layouts(),
    AutoImport({
      resolvers: [
        ElementPlusResolver({
          // 修改 Element-Plus 颜色
          importStyle: 'sass',
        })
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          // 修改 Element-Plus 颜色
          importStyle: 'sass',
        })
      ],
    }),
  ],
  //配置sass
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/assets/scss/globalVar.scss" as *;`,
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    port: 8080,
    disableHostCheck: true,
    // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
    proxy: {
      // 正则表达式写法
      '^/api': {
        target: 'http://nongtalk.natapp1.cc', // 后端服务实际地址
        changeOrigin: true, //开启代理
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})